package com.garbagecan.cachestudy.oscache.simple;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class GroupExample {
	
	private static Log log = LogFactory.getLog(GroupExample.class);
	
	public static void main(String[] args) {
		GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();
		String myKey = "myKey";
		String myValue = "myValue";
		String[] groups = {"mygroup1", "mygroup2"};
		
		try {
			cacheAdmin.putInCache(myKey, myValue, groups);
			log.info(myKey + " = " + cacheAdmin.getFromCache(myKey));
		} catch (NeedsRefreshException ex) {
			log.error("The cache has been flushed!");
			cacheAdmin.cancelUpdate(myKey);
		}

		try {
			log.info("flush mygroup1");
			cacheAdmin.flushGroup("mygroup1");
			log.info(myKey + " = " + cacheAdmin.getFromCache(myKey));
		} catch (NeedsRefreshException ex) {
			log.error("The cache has been flushed!");
			cacheAdmin.cancelUpdate(myKey);
		}

		try {
			log.info("flush mygroup2");
			cacheAdmin.flushGroup("mygroup2");
			log.info(myKey + " = " + cacheAdmin.getFromCache(myKey));
		} catch (NeedsRefreshException ex) {
			log.error("The cache has been flushed!");
			cacheAdmin.cancelUpdate(myKey);
		}
	}
}
